package com.cskaoyan.service;

import com.cskaoyan.bean.data.WxBaseData2;
import com.cskaoyan.bean.param.WxCommentListBO;
import com.cskaoyan.bean.pojo.GoodsComment;
import com.cskaoyan.bean.pojo.GoodsCommentExample;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.wxvo.UserInfo;
import com.cskaoyan.bean.wxvo.WxCommentCountVO;
import com.cskaoyan.bean.wxvo.WxCommentListVO;
import com.cskaoyan.mapper.GoodsCommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    GoodsCommentMapper goodsCommentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public WxCommentCountVO countComment(Integer valueId, Integer type) {

        GoodsCommentExample goodsCommentExample = new GoodsCommentExample();
        GoodsCommentExample.Criteria criteria = goodsCommentExample.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andValueIdEqualTo(valueId);
        List<GoodsComment> goodsComments = goodsCommentMapper.selectByExample(goodsCommentExample);
        int allCount = goodsComments.size();

        GoodsCommentExample goodsCommentExample1 = new GoodsCommentExample();
        GoodsCommentExample.Criteria criteria1 = goodsCommentExample1.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria1.andValueIdEqualTo(valueId);
        criteria1.andHasPictureEqualTo(true);
        List<GoodsComment> goodsComments1 = goodsCommentMapper.selectByExample(goodsCommentExample1);
        int hasPicCount = goodsComments1.size();
        WxCommentCountVO wxCommentCountVO = new WxCommentCountVO();
        wxCommentCountVO.setAllCount(allCount);
        wxCommentCountVO.setHasPicCount(hasPicCount);
        return wxCommentCountVO;

    }

    @Override
    public GoodsComment postComment(GoodsComment goodsComment,Integer userId) {
        Date date = new Date();
        goodsComment.setAddTime(date);
        goodsComment.setUpdateTime(date);
        goodsComment.setUserId(userId);
        goodsComment.setDeleted(false);
        int affectedRows = goodsCommentMapper.insertSelective(goodsComment);
        if(affectedRows>0){
            return goodsComment;
        }
        return null;

    }

    @Override
    public WxBaseData2<WxCommentListVO> queryCommentList(WxCommentListBO wxCommentListBO) {
//        分页
        PageHelper.startPage(wxCommentListBO.getPage(),wxCommentListBO.getSize());
        GoodsCommentExample goodsCommentExample = new GoodsCommentExample();
        GoodsCommentExample.Criteria criteria = goodsCommentExample.createCriteria();
        criteria.andValueIdEqualTo(wxCommentListBO.getValueId());
        criteria.andTypeEqualTo(wxCommentListBO.getType());
        criteria.andDeletedEqualTo(false);
        List<GoodsComment> goodsComments = goodsCommentMapper.selectByExample(goodsCommentExample);
//        拿count
        long count = goodsCommentMapper.countByExample(goodsCommentExample);
        List<WxCommentListVO> wxCommentListVOS = new ArrayList<>();


        for (GoodsComment goodsComment : goodsComments) {
            WxCommentListVO wxCommentListVO = new WxCommentListVO();
            wxCommentListVO.setAddTime(goodsComment.getAddTime());
            wxCommentListVO.setContent(goodsComment.getContent());
            wxCommentListVO.setPicList(goodsComment.getPicUrls());

//            通过userId的集合查询user表拿到userId
            User user = userMapper.selectByPrimaryKey(goodsComment.getUserId());
            UserInfo userInfo = new UserInfo();
            userInfo.setAvatarUrl(user.getAvatar());
            userInfo.setNickName(user.getNickname());
            wxCommentListVO.setUserInfo(userInfo);

            wxCommentListVOS.add(wxCommentListVO);
        }

        return WxBaseData2.ok(wxCommentListVOS,count,wxCommentListBO.getPage());

    }
}
